package com.elvt.server.config.security;

import com.elvt.server.utils.RespSult;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;

/**
 * @Author 作者： elvt
 * @Data 时间：2022/5/22
 * @packageName 包路径： ybg_back_end
 * @Desciption 说明： 当未登录或者token失效的访问接口时，自定义返回结果
 */
@Component
public class   RestAuthorizationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        PrintWriter writer=response.getWriter();
        RespSult bean = RespSult.error("尚未登录，请先登录！");
        bean.setCode(401L);
        writer.write(new ObjectMapper().writeValueAsString(bean));
        writer.flush();
        writer.close();
    }
}
